package com.guslegend.exception;

import com.guslegend.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: guslegend
 * @description: 全局异常处理器
 * @create: 2025-05-25 08:31
 **/
@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {
    /**
     * 返回json格式
     */
    @ResponseBody
    /** 异常处理注解  处理异常的种类*/
    @ExceptionHandler(value = Exception.class)
    public JsonData handleCustomException(Exception e) {
        //是否是业务全局异常
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.error("[业务异常]{}", bizException);
            return JsonData.buildCodeAndMsg(bizException.getCode(), bizException.getMessage());
        } else {
            log.error("[系统其他异常]{}", e);
            return JsonData.buildError(e.getMessage());
        }
    }
}
